package com.peng.leetcode.search;

import java.util.HashMap;
import java.util.Map;

/**
 * TwoSum
 * 167. 两数之和 II - 输入有序数组
 * https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/
 * <p>
 * Created on 2022/12/10
 *
 * @author lupeng
 */
public class TwoSum {

    Map<Integer, Integer> val2IndexMap = new HashMap<>();

    public int[] twoSum(int[] numbers, int target) {
        for (int i = 0; i < numbers.length; i++) {
            if (val2IndexMap.containsKey(target - numbers[i])) {
                return new int[]{val2IndexMap.get(target - numbers[i]) + 1, i + 1};
            } else {
                val2IndexMap.put(numbers[i], i);
            }
        }
        return null;
    }
}
